﻿namespace UFIDA.U9.Cust.SH.SHFastShipBP
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Business;
    using UFIDA.U9.Cust.SH.SHFastShipBE;
    using UFIDA.U9.SM.Ship;
    using UFIDA.U9.ISV.SM.Proxy;
    using UFIDA.U9.Base;
    using UFSoft.UBF.Util.DataAccess;

	/// <summary>
	/// UnApproveBP partial 
	/// </summary>	
	public partial class UnApproveBP 
	{	
		internal BaseStrategy Select()
		{
			return new UnApproveBPImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class UnApproveBPImpementStrategy : BaseStrategy
	{
		public UnApproveBPImpementStrategy() { }

		public override object Do(object obj)
		{						
			UnApproveBP bpObj = (UnApproveBP)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();

            if (bpObj.DocID <= 0) return null;
            SHFastShip doc = SHFastShip.Finder.FindByID(bpObj.DocID);
            if (doc == null) return null;

            using (ISession session = Session.Open())
            {
                if (doc.DocState != FastShipDocState.DocAudited)
                    throw new Exception("单据状态不为已审,不能弃审!");

                UnApproveVerify(doc);

                UnApproveU9ShipDoc(doc);
                DeleteU9ShipDoc(doc);

                UpdateOEM(doc);

                UnApproveFastShipDoc(doc);

                session.Commit();
            }
            return null;

		}

        private static void UnApproveFastShipDoc(SHFastShip doc)
        {
            doc.DocState = FastShipDocState.DocOpen;
            doc.UnApproveMan = Context.LoginUser;
            doc.UnApproveDate = DateTime.Now;
            foreach (SHFastShipLine line in doc.SHFastShipLine)
            {
                line.DocLineState = FastShipDocState.DocOpen;
            }
        }

        private void UpdateOEM(SHFastShip doc)
        {
            StringBuilder sqllist = new StringBuilder();

            if (Context.LoginOrg.Code == "1000")
            {
                foreach (SHFastShipLine line in doc.SHFastShipLine)
                {
                    if (line.LotMasterKey != null)
                    {
                        string sql = "";
                        if (line.Item.Code == "3997030000000")
                        {
                            //补件订单
                            sql = @" update Cust_RepairDocLine set LineStatus=9,DescFlexField_PrivateDescSeg6=null
                                 where JLID='{0}'";
                            sql = string.Format(sql, line.Lot);
                        }
                        else
                        {
                            sql = @"  update Cust_OemMo_Info set Status=2,DescFlexField_PrivateDescSeg1=null,DescFlexField_PrivateDescSeg2=null
                             where ZBH='{0}'";
                            sql = string.Format(sql, line.Lot);
                        }

                        sqllist.AppendLine(sql);
                    }
                }
            }
            if (sqllist.Length > 0)
            {
                DataAccessor.RunSQL(DataAccessor.GetConn(), sqllist.ToString(), null);
            }
        }

        private void UnApproveVerify(SHFastShip doc)
        {
            Ship ship = Ship.Finder.FindByID(doc.ShipDocKey.ID);
            if (ship == null) throw new Exception("出货单查找失败！");

            if (ship.PrintAmount > 0) throw new Exception("出货单已打印销货清单，不能弃审！");
            if (!string.IsNullOrEmpty(ship.DescFlexField.PrivateDescSeg3)) throw new Exception("出货单已打印提货单，不能弃审！");
            if (!string.IsNullOrEmpty(ship.DescFlexField.PrivateDescSeg29)) throw new Exception("出货单已生成装车单，不能弃审！");
            if (!string.IsNullOrEmpty(ship.DescFlexField.PrivateDescSeg2)) throw new Exception("已抛砖总经销收货单，不能弃审！");
            foreach (ShipLine line in ship.ShipLines)
            {
                if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg2)) 
                    throw new Exception("出货单已打印提货单，不能弃审！");
                if (!string.IsNullOrEmpty(line.DescFlexField.PrivateDescSeg3)) 
                    throw new Exception("已抛砖总经销销售订单，不能弃审！");
            }
        }

        private void UnApproveU9ShipDoc(SHFastShip doc)
        {
            Ship u9ship = Ship.Finder.FindByID(doc.ShipDocKey.ID);
            if (u9ship == null) throw new Exception("查找U9出货单失败!");

            if (u9ship.Status == ShipStateEnum.Approved)
            {
                //弃审U9出货单
                ShipmentApprove bp = new ShipmentApprove();
                bp.IsUnApprove = true;
                bp.ShipmentKey = u9ship.Key;
                bp.SysVersion = u9ship.SysVersion;
                bp.Do();
            }
        }

        private void DeleteU9ShipDoc(SHFastShip doc)
        {
            Ship u9ship = Ship.Finder.FindByID(doc.ShipDocKey.ID);
            if (u9ship == null) throw new Exception("查找U9出货单失败!");

            if (u9ship.Status == ShipStateEnum.Approved)
                throw new Exception("U9出货单已审核，不能弃审！");

            DeleteShipProxy proxy = new DeleteShipProxy();
            proxy.ShipIDs = new List<long>();
            proxy.ShipIDs.Add(u9ship.ID);
            proxy.ShipDocNos = new List<string>();
            proxy.ShipDocNos.Add(u9ship.DocNo);
            proxy.Do();

            doc.ShipDocKey = null;
            doc.ShipDocNo = "";
        }		
	}

	#endregion
	
	
}